/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.dinner.web.recharge;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.dinner.entity.balance.ResAccountBalance;
import com.thinkgem.jeesite.modules.dinner.entity.recharge.ResRecharge;
import com.thinkgem.jeesite.modules.dinner.entity.recharge.ResRechargeHis;
import com.thinkgem.jeesite.modules.dinner.service.balance.ResAccountBalanceService;
import com.thinkgem.jeesite.modules.dinner.service.recharge.ResRechargeHisService;
import com.thinkgem.jeesite.modules.dinner.service.recharge.ResRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 充值记录Controller
 * @author dai
 * @version 2018-11-18
 */
@Controller
@RequestMapping(value = "${adminPath}/dinner/recharge/resRecharge")
public class ResRechargeController extends BaseController {

	@Autowired
	private ResRechargeService resRechargeService;
	@Autowired
	private ResRechargeHisService resRechargeHisService;
	@Autowired
	private ResAccountBalanceService resAccountBalanceService;

	/**
	 * 手动充值页面
	 * @param resRecharge
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "manualRechargeForm")
	public String manualRechargeForm(ResRecharge resRecharge, Model model) {
		model.addAttribute("resRecharge", resRecharge);
		return "modules/dinner/recharge/manualRechargeForm";
	}

	/**
	 * 自动充值页面
	 * @param resRecharge
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "autoRechargeForm")
	public String rechargeForm(ResRecharge resRecharge, Model model) {
		model.addAttribute("resRecharge", resRecharge);
		return "modules/dinner/recharge/autoRechargeForm";
	}

	@RequestMapping(value = "manualRecharge")
	public String manualRecharge(ResRecharge resRecharge, Model model, RedirectAttributes redirectAttributes) {
		try {
			if (!beanValidator(model, resRecharge)){
				return manualRechargeForm(resRecharge, model);
			}
			//resRechargeService.save(resRecharge);
			/************往充值记录表添加记录开始************/
			ResRechargeHis resRechargeHis = new ResRechargeHis();
			//设置充值金额
			resRechargeHis.setRechargeAmount(resRecharge.getRechargeAmount());
			ResAccountBalance resAccountBalance = resAccountBalanceService.findByUserId(resRecharge.getUser().getId());
			if(resAccountBalance == null){
				//账户不存在
				resAccountBalance = new ResAccountBalance();
				resAccountBalance.setUser(resRecharge.getUser());
				resAccountBalance.setBalance(0.0);
				resAccountBalanceService.save(resAccountBalance);
			}
			//设置充值前余额
			resRechargeHis.setPreBalance(resAccountBalance.getBalance());
			//设置充值后余额
			double afterBalance = resAccountBalance.getBalance()+resRecharge.getRechargeAmount();
			resRechargeHis.setAfterBalance(afterBalance);
			resRechargeHis.setUser(resRecharge.getUser());
			resRechargeHisService.save(resRechargeHis);
			/************往充值记录表添加记录结束************/
			/*******************更新账户余额******************/
			resAccountBalanceService.updateBalanceByUserId(resRecharge.getUser().getId(),afterBalance);
			addMessage(redirectAttributes, "充值成功");
		}catch (Exception e){
			e.printStackTrace();
		}

		return "redirect:"+Global.getAdminPath()+"/dinner/recharge/resRechargeHis/?repage";
	}

}